<template>
    <page-container title="文章管理">
        <template #extra>
            <el-button type="primary" @click="onAddArticle"> 发布文章 </el-button>
        </template>
        <!-- 表单区域 -->
        <el-form inline>
            <el-form-item label="文章分类：">
                <!-- 
                Vue2中 => v-model :value 和@input的简写
                Vue3中 => v-model :modelValue 和  @update:modelValue 的简写
                vue3中 => v-model:cid是  :cid和 @update:cid的简写
                -->
                <channel-select v-model="params.cate_id"></channel-select>
            </el-form-item>
            <el-form-item label="发布状态：">
                <el-select v-model="params.state">
                    <el-option label="已发布" value="已发布"></el-option>
                    <el-option label="草稿" value="草稿"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
                <el-button @click="onReset">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格区域 -->
        <el-table :data="articleList" style="width: 100%" v-loading="loading">
            <el-table-column label="文章标题" width="400">
                <template #default="{ row }">
                    <el-link type="primary" :underline="false">{{ row.title }}</el-link>
                </template>
            </el-table-column>
            <el-table-column label="分类" prop="cate_name"></el-table-column>
            <el-table-column label="发表时间" prop="pub_date">
                <template #default="{ row }">
                    {{ formatTime(row.pub_date) }}
                </template>
            </el-table-column>
            <el-table-column label="状态" prop="state"></el-table-column>
            <!-- 利用作用域插槽 row获取当前行的数据 => v-for遍历item -->
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="onEditArticle(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="onDeleteArticle(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.pagesize"
            :page-sizes="[2, 3, 4, 5, 10]" layout="jumper, total, sizes, prev, pager, next" :background="true"
            :total="total" @size-change="onSizeChange" @current-change="onCurrentChange"
            style="margin-top: 20px; justify-content: flex-end" />
        <!-- 抽屉 -->
        <article-edit @success="onSuccess" ref="articleEditRef"></article-edit>
    </page-container>
</template>

<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'



// 假数据
const articleList = ref([
])
const total = ref(0)
const loading = ref(false)

//当前每页条数变化时触发的函数
const onSizeChange = (size) => {
    //只要是每页条数变化了，那么原本正在访问的当前页的意义也就不大了，数据大概路已经不在原来的那一页了
    //需要从第一页渲染即可
    params.value.pagenum = 1
    params.value.pagesize = size
    //基于最新的当前页的和每页的条数渲染数据
    getArticleList()
}
//当前页变化时触发的函数
const onCurrentChange = (page) => {
    //更新当前页
    params.value.pagenum = page
    //基于最新的当前页，渲染数据
    getArticleList()
}

const onSearch = () => {
    params.value.pagenum = 1
    getArticleList()
}

const onReset = () => {
    params.value.pagenum = 1
    params.value.cate_id = ''
    params.value.state = ''
    getArticleList()
}

const articleEditRef = ref()
const onAddArticle = () => {
    //visibleDrawer.value = true
    articleEditRef.value.open({})
}
//编辑逻辑
const onEditArticle = (row) => {
    //console.log(row)
    articleEditRef.value.open(row)
}

// 删除逻辑
const onDeleteArticle = async (row) => {
    // 提示用户是否要删除
    await ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
    await artDelService(row.id)
    ElMessage.success('删除成功')
    // 重新渲染列表
    getArticleList()
}
const params = ref({
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
})
const getArticleList = async () => {
    loading.value = true
    const res = await artGetListService(params.value)
    articleList.value = res.data.data
    total.value = res.data.total
    loading.value = false
}
getArticleList()

//添加或者编辑成功的回调
// 添加修改成功
const onSuccess = (type) => {
    if (type === 'add') {
        // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
        const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
        params.value.pagenum = lastPage
    }
    getArticleList()
}
</script>

<style lang="scss" scoped></style>
